package com.course.dao;

import java.util.List;
import java.util.Set;

import com.course.domain.College;
import com.course.domain.Department;
import com.course.domain.MyClass;
import com.course.domain.Specialty;
import com.course.hibernate.HibernateSessionFactory;

public class CollegeDao extends SupperDao
{

	@SuppressWarnings( "unchecked" )
	public long countOfCollege()
	{
		String hql = "select count(*) from College";
		List<Long> list = this.findByHQL( hql );
		return list.get( 0 );
	}

	@SuppressWarnings( "unchecked" )
	public List< College > getColleges( int start, int length )
	{
		String hql = "from College";
		return this.findPaging( hql, start, length, null );
	}

	public boolean deleteCollegeById( int id )
	{
		College college = ( College )this.get( College.class, id );
		return this.delete( college );
	}
	
	@SuppressWarnings( "unchecked" )
	public List< College > getTrees()
	{
		String hql = "from College";
		List< College > list = null;
		try
		{
			session = HibernateSessionFactory.getSession(); // 获取Session对象
			session.beginTransaction(); // 开启事物
			list = session.createQuery( hql ) // 创建Query对象
					.list(); // 返回结果集
			
			for ( College college : list )
			{
				Set< Department > departmnets = college.getDepartments();
				for ( Department department : departmnets )
				{
					Set< Specialty > specialties = department.getSpecialties();
					for ( Specialty specialty : specialties )
					{
						Set< MyClass > myClasses = specialty.getMyClasses();
						for ( MyClass cla : myClasses )
						{
							cla.getId();
						}
					}
				}
			}
			session.getTransaction().commit(); // 提交事物
			
		} catch ( Exception e )
		{
			e.printStackTrace(); // 打印异常信息
			session.getTransaction().rollback(); // 回滚事物
			
		} finally
		{
			session.close();
		}
		return list;
	}

	public boolean saveCollege( String name )
	{
		College college = new College();
		college.setName( name );
		return this.save( college );
	}

	public boolean updateCollege( Integer id, String name )
	{
		College college = ( College )this.get( College.class, id );
		college.setName( name );
		return this.update( college );
	}

	@SuppressWarnings( "unchecked" )
	public List< College > getAllColleges()
	{
		String hql = "from College";
		return this.findByHQL( hql );
	}

	public College getCollegeBySpecialtyId( int specialtyId )
	{
		String hql = "select c from College c, Department d, Specialty s where c.id = d.college.id and d.id = s.department.id and s.id = " + specialtyId;
		return ( College )(this.findByHQL( hql ).get( 0 ));
	}

	@SuppressWarnings( "unchecked" )
	public College getCollegeByDepartmentId( int departmentId )
	{
		List list = null;
		String hql = "select c from College c, Department d where d.college.id = c.id and d.id = " + departmentId;
		list = ( List )this.findByHQL( hql );
		if(list.isEmpty())
			return null;
		return ( College )list.get( 0 );
	}

	@SuppressWarnings( "unchecked" )
	public List< College > searchColleges( int start, int length, String name )
	{
		String hql = "from College c where c.name = '" + name + "'";
		return this.findPaging( hql, start, length, null );
	}

	@SuppressWarnings( "unchecked" )
	public long countOfCollegeByCollegeName( String name )
	{
		String hql = "select count(*) from College c where c.name = '" + name + "'";
		List<Long> list = this.findByHQL( hql );
		return list.get( 0 );
	}
}
